package com.losdun.points.controller;

import com.losdun.common.annotation.Anonymous;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.points.domain.PointsGoods;
import com.losdun.points.domain.PointsGoodsCategory;
import com.losdun.points.domain.PointsOrder;
import com.losdun.points.service.PointsGoodsCategoryService;
import com.losdun.points.service.PointsGoodsService;
import com.losdun.points.service.PointsOrderService;
import com.losdun.weixin.security.WxSecurityUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author Sword.Ye
 * @Date 2025/7/8
 **/
@RestController
@Slf4j
@RequestMapping(value = "/app/pointsStore")
public class AppPointsStoreController extends BaseController {
    @Autowired
    private PointsGoodsService pointsGoodsService;
    @Autowired
    private PointsGoodsCategoryService pointsGoodsCategoryService;
    @Autowired
    private PointsOrderService pointsOrderService;

    @GetMapping("/allCategory")
    @ApiOperation("查询列表")
    @Anonymous
    public AjaxResult allCategory() {
        List<PointsGoodsCategory> list = pointsGoodsCategoryService.queryAllCategory();
        return AjaxResult.success().put("data",list);
    }

    @GetMapping("/listGoods")
    @ApiOperation("查询列表")
    @Anonymous
    public TableDataInfo listGoods(PointsGoods goods) {
        startPage();
        List<PointsGoods> list= pointsGoodsService.queryList(goods);
        return getDataTable(list);
    }

    @GetMapping("/goodsDetail")
    @Anonymous
    public AjaxResult goodsDetail(Long id) {
        PointsGoods goods = pointsGoodsService.getById(id);
        return AjaxResult.success().put("data",goods);
    }


    @GetMapping("/order/list")
    public TableDataInfo orderList() {
        startPage();
        PointsOrder order=new PointsOrder();
        order.setUserId(WxSecurityUtils.getUserId());
        List<PointsOrder> list= pointsOrderService.queryList(order);
        return getDataTable(list);
    }

    @PostMapping("/order/save")
    public AjaxResult orderSave(@RequestBody PointsOrder order) {
        try {
            order.setUserId(WxSecurityUtils.getUserId());
            pointsOrderService.insertOrder(order);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }


    @PostMapping("/order/cancel")
    public AjaxResult orderCancel(Long orderId) {
        try {
            pointsOrderService.cancelOrder(orderId,WxSecurityUtils.getUserId());
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("订单取消成功");
    }

    @GetMapping("/order/detail")
    public AjaxResult orderDetail(Long orderId) {
        try {
            PointsOrder order=pointsOrderService.getDetail(orderId,WxSecurityUtils.getUserId());
            return AjaxResult.success().put("data",order);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }

    }


    @PostMapping("/order/received")
    public AjaxResult orderReceived(Long orderId) {
        try {
            pointsOrderService.orderConfirmReceived(orderId,WxSecurityUtils.getUserId());
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("收货成功");
    }
}
